namespace Kodefabrikken.DDD.Entity.Experimental
{
    public class KeyedEntity<TKey, TSubClass> : IKeyedEntity<TKey> where TSubClass : KeyedEntity<TKey, TSubClass>
    {
        public class Identity : Identity<TKey, Identity>
        {
            public Identity(TKey id)
                : base(id)
            {
            }
        }

        public KeyedEntity(TKey id)
        {
            this.AssertOfType<TSubClass>();
               // We need this assertion, if not, something like this might happen
               // class SomeId : Identity<int, SomeId> ... class SomeId2 : Identity<int, SomeId>
               // This will compile, but produce incorrect result with no clue to the user

            fId = new KeyedEntity<TKey, TSubClass>.Identity(id);
        }

        private readonly Identity fId;
        public IIdentity<TKey> Key
        {
            get { return fId; }
        }

        #region IEquatable<IKeyedEntity<TKey>>

        public bool Equals(IKeyedEntity<TKey> other)
        {
            return Key.Equals(other.Key);
        }

        #endregion
    }
}
